package com.nhatviet.webservice.helpers;

import java.util.List;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.nhatviet.webservice.models.Category;
import com.nhatviet.webservice.models.Contact;
import com.nhatviet.webservice.models.IconInfo;
import com.nhatviet.webservice.models.Location;
import com.nhatviet.webservice.models.Venue;
import com.nhatviet.webservice.models.VenueHereNow;
import com.nhatviet.webservice.models.VenueSpecical;
import com.nhatviet.webservice.models.VenueStatistic;

public class JSONVenueParse
{
	/* On the top of JSON string */
	public final static String KEY_RESPONSE = "response";
	public final static String KEY_VENUES = "venues";
	/* For a venue */
	public final static String KEY_VENUE_ID = "id";
	public final static String KEY_VENUE_NAME = "name";
	public final static String KEY_VENUE_CONTACT = "contact";
	public final static String KEY_VENUE_LOCATION = "location";
	public final static String KEY_VENUE_CATEGORIES = "categories";
	public final static String KEY_VENUE_VERIFIED = "verified";
	public final static String KEY_VENUE_STATS = "stats";
	public final static String KEY_VENUE_SPECIALS = "specials";
	public final static String KEY_VENUE_HERE_NOW = "hereNow";
	/* For a venue contact */
	public final static String KEY_VENUE_CONTACT_PHONE = "phone";
	public final static String KEY_VENUE_CONTACT_FORMATTED_PHONE = "formattedPhone";
	public final static String KEY_VENUE_CONTACT_TWITTER = "twitter";
	/* For a venue location */
	public final static String KEY_VENUE_LOCATION_ADDRESS = "address";
	public final static String KEY_VENUE_LOCATION_CROSS_STREET = "crossStreet";
	public final static String KEY_VENUE_LOCATION_LATITUDE = "lat";
	public final static String KEY_VENUE_LOCATION_LONGITITUDE = "lng";
	public final static String KEY_VENUE_LOCATION_DISTANCE = "distance";
	public final static String KEY_VENUE_LOCATION_CITY = "city";
	public final static String KEY_VENUE_LOCATION_COUNTRY = "country";
	public final static String KEY_VENUE_LOCATION_POSTAL_CODE = "postalCode";
	public final static String KEY_VENUE_LOCATION_STATE = "state";
	/* For a venue category */
	public final static String KEY_VENUE_CATEGORY_ID = "id";
	public final static String KEY_VENUE_CATEGORY_NAME = "name";
	public final static String KEY_VENUE_CATEGORY_PLURAL_NAME = "pluralName";
	public final static String KEY_VENUE_CATEGORY_SHORT_NAME = "shortName";
	public final static String KEY_VENUE_CATEGORY_ICON = "icon";
	public final static String KEY_VENUE_CATEGORY_PRIMARY = "primary";
	/* For a venue statistic */
	public final static String KEY_VENUE_STAT_CHECKINS_COUNT = "checkinsCount";
	public final static String KEY_VENUE_STAT_USERS_COUNT = "usersCount";
	public final static String KEY_VENUE_STAT_TIP_COUNT = "tipCount";
	/* For a venue special */
	public final static String KEY_VENUE_SPECIAL_COUNT = "count";
	public final static String KEY_VENUE_SPECIAL_ITEM = "item";
	/* For a venue hereNow */
	public final static String KEY_VENUE_HERE_NOW_COUNT = "count";
	/* For a venue category icon */
	public final static String KEY_VENUE_CATEGORY_ICON_PREFIX = "prefix";
	public final static String KEY_VENUE_CATEGORY_ICON_SIZES = "sizes";
	public final static String KEY_VENUE_CATEGORY_ICON_NAME = "name";

	private String jsonStr = null;

	public JSONVenueParse(String jsonStr)
	{
		this.jsonStr = jsonStr;
	}

	public Vector<Venue> execute()
	{
		Vector<Venue> listVenue = new Vector<Venue>();
		try
		{
			JSONObject object = new JSONObject(jsonStr);
			JSONObject responseJSONObj = object.getJSONObject(KEY_RESPONSE);
			JSONArray venuesArrJSONObj = responseJSONObj.getJSONArray(KEY_VENUES);
			int indexVenue = 0;
			while (true)
			{
				Log.d("DEBUG", "+ Venue " + indexVenue);
				try
				{
					JSONObject venueObj = venuesArrJSONObj.getJSONObject(indexVenue++);
					// If venueObj null then break
					// else
					Venue venue = new Venue();
					/* Get attributes of the venue */
					venue.setId(JSONHelper.getString(venueObj, KEY_VENUE_CATEGORY_ID));
					venue.setName(JSONHelper.getString(venueObj, KEY_VENUE_CATEGORY_NAME));
					venue.setVerified(JSONHelper.getBoolean(venueObj, KEY_VENUE_VERIFIED));
					/* Get the contact object of the venue */
					JSONObject contactJsonObject = JSONHelper.getJSONObject(venueObj, KEY_VENUE_CONTACT);
					if (contactJsonObject != null)
					{
						Contact contact = new Contact();
						contact.setPhone(JSONHelper.getString(contactJsonObject, KEY_VENUE_CONTACT_PHONE));
						contact.setFormattedPhone(JSONHelper.getString(contactJsonObject, KEY_VENUE_CONTACT_FORMATTED_PHONE));
						contact.setTwitter(JSONHelper.getString(contactJsonObject, KEY_VENUE_CONTACT_TWITTER));
						venue.setContact(contact);
					}
					/* Get the object location of the venue */
					JSONObject locationJsonObject = JSONHelper.getJSONObject(venueObj, KEY_VENUE_LOCATION);
					if (locationJsonObject != null)
					{
						Location location = new Location();
						location.setAddress(JSONHelper.getString(locationJsonObject, KEY_VENUE_LOCATION_ADDRESS));
						location.setCity(JSONHelper.getString(locationJsonObject, KEY_VENUE_LOCATION_CITY));
						location.setCountry(JSONHelper.getString(locationJsonObject, KEY_VENUE_LOCATION_COUNTRY));
						location.setCrossStreet(JSONHelper.getString(locationJsonObject, KEY_VENUE_LOCATION_CROSS_STREET));
						location.setDistance(JSONHelper.getInt(locationJsonObject, KEY_VENUE_LOCATION_DISTANCE));
						location.setLatitude(JSONHelper.getDouble(locationJsonObject, KEY_VENUE_LOCATION_LATITUDE));
						location.setLongitude(JSONHelper.getDouble(locationJsonObject, KEY_VENUE_LOCATION_LONGITITUDE));
						location.setPostalCode(JSONHelper.getString(locationJsonObject, KEY_VENUE_LOCATION_POSTAL_CODE));
						location.setState(JSONHelper.getString(locationJsonObject, KEY_VENUE_LOCATION_STATE));
						venue.setLocation(location);
					}
					/* Get the object of the statistic of the venue */
					JSONObject statisticJsonObject = JSONHelper.getJSONObject(venueObj, KEY_VENUE_CONTACT);
					if (statisticJsonObject != null)
					{
						VenueStatistic statistic = new VenueStatistic();
						statistic.setCheckinsCount(JSONHelper.getInt(statisticJsonObject, KEY_VENUE_STAT_CHECKINS_COUNT));
						statistic.setTipCount(JSONHelper.getInt(statisticJsonObject, KEY_VENUE_STAT_TIP_COUNT));
						statistic.setUsersCount(JSONHelper.getInt(statisticJsonObject, KEY_VENUE_STAT_USERS_COUNT));
						venue.setStats(statistic);
					}
					/* Get the object of the special of the venue */
					JSONObject specialJsonObject = JSONHelper.getJSONObject(venueObj, KEY_VENUE_CONTACT);
					if (specialJsonObject != null)
					{
						VenueSpecical specical = new VenueSpecical();
						specical.setCount(JSONHelper.getInt(specialJsonObject, KEY_VENUE_SPECIAL_COUNT));
						// get the Items list
						// List<VenueSpecicalItem> itemList = new
						// Vector<VenueSpecicalItem>();
						// JSONArray itemJsonArray =
						// specialJsonObject.getJSONArray(KEY_VENUE_SPECIAL_ITEM);
						// while (true)
						// {
						// try
						// {
						// VenueSpecicalItem item = new VenueSpecicalItem();
						// // get item......
						// throw new JSONException("");
						// } catch (JSONException e)// end of the array
						// {
						// break;
						// }
						// }
						// specical.setItems(itemList);

						venue.setSpecials(specical);
					}
					/* Get the object of the hereNow of the venue */
					JSONObject hereNowJsonObject = JSONHelper.getJSONObject(venueObj, KEY_VENUE_CONTACT);
					if (hereNowJsonObject != null)
					{
						VenueHereNow hereNow = new VenueHereNow();
						hereNow.setCount(JSONHelper.getInt(hereNowJsonObject, KEY_VENUE_HERE_NOW_COUNT));
						venue.setHereNow(hereNow);
					}
					/* Get the list of categories of the venue */
					JSONArray categoryJsonArray = JSONHelper.getJSONArray(venueObj, KEY_VENUE_CATEGORIES);
					if (categoryJsonArray != null)
					{
						List<Category> categoryList = new Vector<Category>();
						int indexCategory = 0;
						while (true)
						{
							try
							{
								Category category = new Category();
								JSONObject categoryJsonObject = categoryJsonArray.getJSONObject(indexCategory++);
								category.setId(JSONHelper.getString(categoryJsonObject, KEY_VENUE_CATEGORY_ID));
								category.setName(JSONHelper.getString(categoryJsonObject, KEY_VENUE_CATEGORY_NAME));
								category.setPluralName(JSONHelper.getString(categoryJsonObject, KEY_VENUE_CATEGORY_PLURAL_NAME));
								category.setPrimary(JSONHelper.getBoolean(categoryJsonObject, KEY_VENUE_CATEGORY_PRIMARY));
								category.setShortName(JSONHelper.getString(categoryJsonObject, KEY_VENUE_CATEGORY_SHORT_NAME));
								// Get ICON
								JSONObject iconJsonObject = JSONHelper.getJSONObject(categoryJsonObject, KEY_VENUE_CATEGORY_ICON);
								if (iconJsonObject != null)
								{
									IconInfo icon = new IconInfo();
									icon.setName(JSONHelper.getString(iconJsonObject, KEY_VENUE_CATEGORY_ICON_NAME));
									icon.setPrefix(JSONHelper.getString(iconJsonObject, KEY_VENUE_CATEGORY_ICON_PREFIX));
									JSONArray sizeJsonArray = JSONHelper.getJSONArray(iconJsonObject, KEY_VENUE_CATEGORY_ICON_SIZES);
									if (sizeJsonArray != null)
									{
										final int sizeLength = 5;
										int[] sizes = new int[sizeLength];
										try
										{
											for (int i = 0; i < sizeLength; i++)
											{
												sizes[i] = sizeJsonArray.getInt(i);
											}
										} catch (JSONException e)
										{
											sizes = null;
										}
										icon.setSizes(sizes);
									}
								}
							} catch (JSONException e)// end of the array
							{
								Log.d("DEBUG", "--------------End of the array of categories!-----------");
								break;
							}
						}
						venue.setCategory(categoryList);
					}
					/* Add the venue */
					listVenue.add(venue);
				} catch (JSONException e)
				{
					Log.d("DEBUG", "-----End of the array of venues!------");
					break;
				}
			}

		} catch (JSONException e)
		{
			e.printStackTrace();
		}
		return listVenue;
	}
}
